using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace LiveStock.dataAccess
{
    /// <summary>
    /// Data Access Layer class for RatingTypes table
    /// </summary>
    public class RatingTypesData
    {
        private RatingTypesData() { }

        public static int RatingTypesInsert(RatingTypes.RatingTypesRow rtr)
        {
            int affectedRows = 0;
            using (SqlCommand cmd = DBManager.GetCommand("RatingTypesInsert"))
            {
                cmd.Parameters.AddWithValue("Name", rtr.Name);
                cmd.Parameters.AddWithValue("Value", rtr.Value);
                affectedRows = DBManager.ExecuteNonQuery(cmd);
            }
            return affectedRows;
        }

        public static int RatingTypeUpdateByID(RatingTypes.RatingTypesRow rtr)
        {
            int affectedRows = 0;
            using (SqlCommand cmd = DBManager.GetCommand("RatingTypeUpdateByID"))
            {
                cmd.Parameters.AddWithValue("RatingTypeID", rtr.RatingTypeId);
                cmd.Parameters.AddWithValue("Name", rtr.Name);
                cmd.Parameters.AddWithValue("Value", rtr.Value);
                affectedRows = DBManager.ExecuteNonQuery(cmd);
            }
            return affectedRows;
        }

        public static RatingTypes.RatingTypesDataTable RatingTypesGetAll()
        {
            RatingTypes.RatingTypesDataTable rtdt = new RatingTypes.RatingTypesDataTable();
            using (SqlCommand cmd = DBManager.GetCommand("RatingTypesGetAll"))
            {
                DBManager.FillDataTable(rtdt, cmd);
            }
            return rtdt;
        }

        public static RatingTypes.RatingTypesDataTable RatingTypeGetByID(int ratingTypeID)
        {
            RatingTypes.RatingTypesDataTable rtdt = new RatingTypes.RatingTypesDataTable();
            using (SqlCommand cmd = DBManager.GetCommand("RatingTypeGetByID"))
            {
                cmd.Parameters.AddWithValue("RatingTypeID", ratingTypeID);
                DBManager.FillDataTable(rtdt, cmd);
            }
            return rtdt;
        }

        public static int RatingTypeDeleteByID(int ratingTypeID)
        {
            int ar = 0;
            using (SqlCommand cmd = DBManager.GetCommand("RatingTypeDeleteByID"))
            {
                cmd.Parameters.AddWithValue("RatingTypeID", ratingTypeID);
                ar = DBManager.ExecuteNonQuery(cmd);
            }
            return ar;
        }

        public static int RatingTypeIDGetByRating(int rating)
        {
            int ratingTypeID = 0;
            RatingTypes.RatingTypesDataTable theRatingTypesDataTable = RatingTypesData.RatingTypesGetAll();
            if (theRatingTypesDataTable.Rows.Count > 0)
            {
                //Set ratingTypeID in case our rating is greater than initial rating in DB.
                ratingTypeID = theRatingTypesDataTable[0].RatingTypeId;
                foreach (RatingTypes.RatingTypesRow ratingRow in theRatingTypesDataTable)
                {
                    if (rating > ratingRow.Value)
                    {
                        //Exit when our rating is greater than the rows rating, gives us our rating from DB. 
                        break;
                    }
                    ratingTypeID = ratingRow.RatingTypeId;
                }
            }
            return ratingTypeID;
        }
    }
}
